<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
use app\admin\model\Admin as AdminModel;

class News extends Common
{	
    public function index($page=1)
    {	
		$each = 10;  //每页显示多少条
        @$dbtable = news;	//数据表名
    	$pages = new AdminModel();
    	$res = $pages->page($page,$each,$dbtable);
    	if($res==-1){
    		$this->error('没有此页面');
    	}
    	$this->assign(array(
    		'list' => $res['list'],
    		'pagetotal' => $res['pagetotal'],
    		'page' => $res['page'],
    	));
        return view();
    }

    public function add()
    {	
    	// 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('pic');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $news = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/news','');	        
	        if($news){
	            $image='/public/uploads/news/'.$news->getFilename();
	            $_POST['pic'] = $image;
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }

	    if(request()->isPost()){
        	if(isset($_POST['position'])){
    			$_POST['position'] = implode(',',$_POST['position']);
    		}
    		$_POST['time'] = date('Y-m-d',time());
    		$res = db('news')->insert($_POST);
    		if($res){
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败');
    		}
    	}
        return view();
    }

    public function edit($id)
    {

    	// 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('pic');
	    // 移动到框架应用根目录/public/uploads/ 目录下
	    if($file){
	        $news = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/news','');	        
	        if($news){
	            $image='/public/uploads/news/'.$news->getFilename();
        		$_POST['pic'] = $image;
        		$pic = db('news')->where('id',$id)->value('pic');	//获取原来图片地址
        		//删除原来的logo图片
		    	$thumbpath=$_SERVER['DOCUMENT_ROOT'].$pic;
		    	if(file_exists($thumbpath)){
                	@unlink($thumbpath);
                }
	        }else{
	            // 上传失败获取错误信息
	            echo $file->getError();
	        }
	    }

	    if(request()->isPost()){
        	if(isset($_POST['position'])){
    			$_POST['position'] = implode(',',$_POST['position']);
    		}
    		//$_POST['time'] = date('Y-m-d',time());	//不需要更新时间
    		$res = db('news')->update($_POST);
    		if($res){
    			
    			$this->success('修改成功','News/index');
    		}else{
    			$this->error('修改失败');
    		}
    	}

	    $list = db('News')->where('id',$id)->find();
    	$this->assign('list',$list);
    	return view();
    }

    public function del($id) 
    {
    	$pic = db('news')->where('id','in',$id)->select();
        $res = db('news')->delete($id);
        if ($res) {
        	foreach ($pic as $k => $v) {
        		$thumbpath=$_SERVER['DOCUMENT_ROOT'].$v['pic'];
		    	if(file_exists($thumbpath)){
	            	@unlink($thumbpath);
	            }
        	}
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function sorts($id,$sort)
    {
		foreach ($id as $k => $v) {
    		$res[] = db('news')->where('id',$id[$k])->update(['sort' => $sort[$k]]);
    	}
    	if (in_array(1, $res)) {
			$this->success('排序成功');
		}else{
			$this->error('排序失败'	);
		}
    }

    public function search($page=1)
    {
    	$each = 10;  //每页显示多少条
        @$dbtable = news;	//数据表名
    	$pages = new AdminModel();
    	$res = $pages->search($page,$each,$dbtable,$_POST);
    	if($res==-1){
    		$this->error('没有此页面');
    	}
    	$this->assign(array(
    		'list' => $res['list'],
    		'pagetotal' => $res['pagetotal'],
    		'page' => $res['page']
    	));
        return view();
    }

}
